package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

/*
类简介：

功能：

时间：2020/4/6

*/
@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService{

    @Autowired
    OrderDao orderDao;

    @Autowired
    OrderSettingDao orderSettingDao;

    //判断预约是否已满
    @Override
    public int isFull(Date orderDate) {
        return orderSettingDao.ifFull(orderDate);
    }

    @Override
    public Map<String,Object> findById(int id) {
        return orderDao.findById(id);
    }

    @Override
    public int addOrder(Order order) {

        //判断是否重复
        if (orderDao.isExit(order) > 0){
            throw new RuntimeException("不可重复预约");
        }

        if (orderDao.addOrder(order) == 0){
            throw new RuntimeException("插入数据库失败");
        }

        //成功后在预约日历上加一
        OrderSetting orderSetting = new OrderSetting();
        orderSetting.setOrderDate(order.getOrderDate());
        orderSettingDao.addOneOrder(orderSetting);

        return order.getId();
    }
}
